package com.neusoft.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.po.CommonResult;
import com.neusoft.po.Token;
import com.neusoft.service.AuthService;

@RefreshScope
@RestController
@RequestMapping("/AuthController")
public class AuthController {
	
	@Autowired
	private AuthService authService;

    @GetMapping("/token/{userId}")
    public CommonResult getToken(@PathVariable("userId") String userId) {
    	String token = authService.getToken(userId);
    	if (token != null)
    		return new CommonResult(200, "success", token);
    	return new CommonResult(403, "forbidden", null);
    }
    
    @GetMapping("/authentication/{token}")
    public CommonResult checkToken( @PathVariable("token") String token) {
    	Token T = authService.checkValid(token);
    	if (T != null) {
    		return new CommonResult(200, "success", T);
    	}
    	return new CommonResult(401, "unauthorized", null);
    }
}